define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'appconfig/index' + location.search,
                    add_url: 'appconfig/add',
                    edit_url: 'appconfig/edit',
                    //del_url: 'appconfig/del',
                    //multi_url: 'appconfig/multi',
                    //import_url: 'appconfig/import',
                    table: 'app_config'
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='config_type']", form).addClass("selectpage").data("source", "appconfigtype/index").data("primaryKey", "id").data("field", "name").data("pageSize", 20);
                Form.events.cxselect(form);

                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'app_id', title: __('租户编号'), visible: false, align: 'left'},
                        {field: 'app.app_name', title: __('租户'), searchable: false, align: 'left'},
                        {
                            field: 'config_type',
                            title: __('配置组'),
                            visible: false,
                            operate: '='
                        },
                        {field: 'config_name', title: __('变量名称'), align: 'left', operate: 'LIKE'},
                        {field: 'config_desc', title: __('中文名称'), operate: 'LIKE'},
                        //{field: 'config_purview', title: __('Config_purview')},
                        {field: 'remark', title: __('备注'), align: 'left', operate: 'LIKE'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            var query = $('#c-config_value_type');
            query.change(function () {
                Controller.buildAddConfigValueDom(query.val());
            });
            Controller.buildAddConfigValueDom(query.val());
        },
        buildAddConfigValueDom: function (config_type) {
            Fast.api.ajax({
                url: 'appconfig/buildAddConfigValueDom',
                loading: false,
                data: {config_type: config_type},
                success: function (res) {
                    $('#c-config_value_dom').html(res.data);
                    Controller.api.bindevent();
                }
            });
        },
        edit: function () {
            var query1 = $('#c-config_id');
            var query2 = $('#c-config_value_type');
            query2.change(function () {
                Controller.buildEditConfigValueDom(query1.val(), query2.val());
            });
            Controller.buildEditConfigValueDom(query1.val(), query2.val());
        },
        buildEditConfigValueDom: function (config_id, config_type) {
            Fast.api.ajax({
                url: 'appconfig/buildEditConfigValueDom',
                loading: false,
                data: {config_id: config_id, config_type: config_type},
                success: function (res) {
                    $('#c-config_value_dom').html(res.data);
                    Controller.api.bindevent();
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
